package edu.gatech.cs2340.aperturescience;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import edu.gatech.cs2340.aperturescience.backend.category.Category;
import edu.gatech.cs2340.aperturescience.backend.task.Task;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * CategoryListAdapter oversees the mingling between CategoryViewActivity and its respective xml file
 * 
 * @author Aperture Science
 * @3/19/12
 */
public class CategoryListAdapter extends ArrayAdapter<Category>{
	Context context;  
    int layoutResourceId;    
    Category[] data = null;
    
    public CategoryListAdapter(Context context, int layoutResourceId, Category[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    /**
     * View returns the appropriate view
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        CategoryListData listData= null;
        
        if(row == null)
        {
            LayoutInflater inflater = ((android.app.Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            listData = new CategoryListData();
            listData.category = (TextView)row.findViewById(R.id.category_item);
            row.setTag(listData);
        }
        else
        {
        	listData = (CategoryListData) row.getTag();
        }
        
        Category category = data[position];
        
        if(category.getCategory().compareTo("All") == 0)
        {
        	row.setBackgroundResource(R.color.Light_Yellow);
        }
        else if(category.getCategory().compareTo("Today") == 0)
        {
        	row.setBackgroundResource(R.color.Light_Blue);
        }
        else if(category.getCategory().compareTo("Completed") == 0)
        {
        	row.setBackgroundResource(R.color.Light_Green);
        }
        else 
        {
        	/*
            int remainder = position%4;
                
        	switch(remainder)
            {
            case 3: row.setBackgroundResource(R.color.Light_Orange); break;
            case 2: row.setBackgroundResource(R.color.Light_Blue); break;
            case 1: row.setBackgroundResource(R.color.Light_Yellow); break;
            default: row.setBackgroundResource(R.color.Light_Red); break;
            }
            */
            
            row.setBackgroundResource(R.color.White);
        }
        
        
        
        
//        SimpleDateFormat date = new SimpleDateFormat("MMM d");
//        DateFormat time = android.text.format.DateFormat.getTimeFormat(context);
        listData.category.setText(category.category);
        return row;
    }
    static class CategoryListData {
    	TextView category;
    }
}
